<?php

class ActionMetals extends BaseAction
{
    public function run()
    {
        if ( Yii::app()->request->getIsPostRequest() )
        {
            $iUserId = Yii::app()->request->getParam('user_id');
            $oUser   = User::model()->findByPk($iUserId);

            $sDoRequest = Yii::app()->request->getParam('do');

            if ( $oUser )
            {
                if ( $sDoRequest == 'change_status' )
                {
                    if($oUser->status == 'active')
                    {
                        $oUser->status = 'inactive';
                        $oUser->save();
                        Yii::app()->user->setFlash('user_change_status_success', 'Пользователь отключен.');
                    }
                    else
                    {
                        $oUser->status = 'active';
                        $oUser->save();
                        Yii::app()->user->setFlash('user_change_status_success', 'Пользователь активирован.');
                    }
                }

                if ( $sDoRequest == 'reset_password' )
                {
                    $sHash = substr(md5(Yii::app()->params['password_sold'].$oProfile->email), 0, 8);

                    $oUser->user_password = md5($sHash);
                    $oUser->save();
                    Yii::app()->user->setFlash('user_reset_password_success', 'Пароль пользователя сброшен.');

                    $oEmailNotification = new EmailNotification();
                    $oEmailNotification->sendEmailToUserResetPassword($oUser, $sHash);
                }
            }

            Yii::app()->controller->redirect(Yii::app()->controller->createUrl('/admin/users/metals'));
        }

        $this->template->oDataProvider = User::getUsersMetalsDataProvider();
    }
}